package com.cskaoyan.market.controller.wx;

import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.wx.WxIndexService;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.OrderVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


/**
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2024/5/18 9:59
 * @Version 1.0
 */
@RestController
@RequestMapping("/wx/user")
public class WxUserController {

    @Autowired
    SecurityManager securityManager;
    @Autowired
    WxIndexService wxIndexService;


    @GetMapping("index")
    //当前接口需要认证才可以访问
    @RequiresAuthentication
    public Object index(){
        //获取自定义的请求头的值
        //直接从session域中获取数据
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketUser user = (MarketUser) session.getAttribute("user");
        OrderVo order = wxIndexService.showIndex(user.getId());
        Map<String ,OrderVo> map=new HashMap<String ,OrderVo>();
        map.put("order", order);
        //业务要求是返回订单的数量，前提是要求能够知道当前的会话是谁，所以需要从session域里面获取数据
       return ResponseUtil.ok(map);
    }
}
